local rende = fk.CreateSkill {

  name = "joyex__rende",

  tags = { Skill.Compulsory, },

}



rende:addEffect("active", {
  name = "joyex__rende",
  anim_type = "support",
  card_filter = function(self, player, to_select, selected)
    return Fk:currentRoom():getCardArea(to_select) ~= Card.PlayerEquip
  end,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player.id and Fk:currentRoom():getPlayerById(to_select):getMark("_joyex__rende-phase") == 0
  end,
  target_num = 1,
  min_card_num = 1,
  on_use = function(self, room, effect)
    local target = effect.tos[1]
    local player = effect.from
    local cards = effect.cards
    local marks = player:getMark("_joyrende_cards-phase")
    room:moveCardTo(cards, Player.Hand, target, fk.ReasonGive, rende.name, nil, false)
    room:addPlayerMark(player, "_joyrende_cards-phase", #cards)
    room:addPlayerMark(target, "_joyex__rende-phase", 1)
    if room:askForSkillInvoke(player,rende.name,nil,"#joyex__rengde-buff::"..target.id) then
      local mark = target:getTableMark("joyex__rende_buff")
      table.insert(mark,player.id)
      room:setPlayerMark(target,"joyex__rende_buff",mark)
    end
    if marks < 2 and marks + #cards >= 2 then
      U.askForUseVirtualCard(room, player, U.getAllCardNames("bt"), nil, rende.name, "#joyex__rende-ask", true, false, false, false)
    end
  end,
})

rende:addEffect(fk.TurnStart, {
  name = "#joyex__rende_trigger",
  anim_type = "control",
  mute = true,
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    return target == player
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      local mark = p:getTableMark("joyex__rende_buff")
      local mark2 = {}
      if table.contains(mark,player.id) then
        for _, j in ipairs(mark) do
          if j ~= player.id then
            table.insert(mark2,j)
          end
        end
        room:setPlayerMark(p,"joyex__rende_buff",mark2)
      end
    end
  end,
})

rende:addEffect("prohibit", {
  name = "#joyex__rende_buff",
  frequency = Skill.Compulsory,
  is_prohibited = function(self, from, to, card)
    local m = from:getTableMark("joyex__rende_buff")
    if table.contains(m,to.id) then
      return card.trueName == "slash" and card.color == Card.Red
    end
  end,
})

return rende